package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.gulimall.product.vo.AttrResponseVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品属性
 *
 * @author ct_start
 * @email Chen_Tao1023@126.com
 * @date 2021-09-12 12:28:14
 *
 * http://localhost:88/api/product/attr/base/list/0?t=1634868573339&page=1&limit=10&key=
 *
 * // 查询属性详情
 * /product/attr/info/{attrId}
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

///product/attrgroup/attr/relation/delete




    /**
     * 获取关联属性的列表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    // attrType 是代表的属性表的意思, 如果是1则代表基本属性,2则是销售属性
    public R baseAttrList(@RequestParam Map<String,Object> map,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType") String type){ // 请求会携带分页参数

        PageUtils page=  attrService.queryBaseAttrPage(map,catelogId,type);
        return  R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /** http://localhost:88/api/product/attr/info/6?t=1635088005657
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);

        AttrResponseVo responseVo =attrService.getAttrInfo(attrId);
        return R.ok().put("attr", responseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
